package main

import (
	"fmt"
	"github.com/gorilla/websocket"
	"net/http"
	"reflect"
	"strconv"
)

var (
	upgrader = websocket.Upgrader{

		CheckOrigin: func(r *http.Request) bool {

			return true
		},
	}
)

func main() {

	http.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {

		for k, v := range request.Header {

			fmt.Println(k, v)
		}

		//writer.Header().Add("Upgrade","websocket")

		writer.WriteHeader(101)

		conn, _ := upgrader.Upgrade(writer, request, nil)

		for i := 0; i < 100; i++ {

			conn.WriteJSON(map[string]string{"index": strconv.Itoa(i)})
		}

		var json map[string]interface{}

		for true {

			readJSONErr := conn.ReadJSON(&json)

			if readJSONErr != nil {

				fmt.Println("readJSONErr : " , readJSONErr)
			}

			fmt.Println("json:", json)

			i := json["aa"]

			itype := reflect.TypeOf(i)
			fmt.Println(itype)
		}

	})

	http.ListenAndServe("localhost:1234", nil)

}
